package com.kjzy.mapper;

import com.kjzy.pojo.Devices;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;
@Mapper
public interface DevicesMapper {
    //增加设备
    @Insert("insert into devices(name,time,num,money) values(#{name},#{time},#{num},#{money})")
    int devicesInsert(Devices devices);
    //删除设备
    @Delete("delete form devices where id=#{id}")
    int devicesDelete(int id);
    //查找设备
    @SelectProvider(type = DevicesSqlBuilder.class,method = "buildFindAllD")
    List<Devices> findAllD(Devices devices);
    @Select("select * from devices where id=#{id}")
    Devices findByIdD(int id);
    //更改
    @Update("update devices set name=#{name},time=#{time},num=#{num},money=#{money} where id=#{id}")
    int devicesUpdate(Devices devices);
    class DevicesSqlBuilder{
        public static String buildFindAllD(final Devices devices){
            return new SQL(){{
                SELECT("*");
                FROM("devices");
                if (devices.getId()!=null && !devices.getId().equals("")){
                    WHERE("id=#{id}");
                }
                if (devices.getName()!=null && !devices.getName().equals("")){
                    WHERE("name LIKE CONCAT('%', #{name},'%')");
                }
            }}.toString();
        }
    }
}
